package com.sunday.pinxinghui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sunday.pinxinghui.R;
import com.sunday.pinxinghui.entity.ServicePlace;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/8/23.
 */

public class ServicePlaceAdapter extends RecyclerView.Adapter {


    private Context mContext;
    private List<ServicePlace> dataSet;
    private boolean isFromSelect;
    private int selectIndex=-1;
    private View.OnClickListener onClickListener;

    public ServicePlaceAdapter(Context context, List<ServicePlace> datas, boolean isFromSelect) {
        this.mContext = context;
        this.dataSet = datas;
        this.isFromSelect = isFromSelect;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.list_service_place_item, null);
        RecyclerView.LayoutParams params=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(params);
        return new ListHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ServicePlace item=dataSet.get(position);
        ListHolder listHolder= (ListHolder) holder;
        listHolder.btnSelect.setVisibility(isFromSelect?View.VISIBLE:View.GONE);
        if (position==selectIndex){
            listHolder.btnSelect.setImageResource(R.mipmap.cart_selected);
        }else {
            listHolder.btnSelect.setImageResource(R.mipmap.cart_unselect);
        }
        listHolder.name.setText(item.getName());
        listHolder.phoneNumber.setText(item.getMobile());
        listHolder.address.setText(item.getAddress());
        listHolder.distance.setText(item.getDistance()+"km");
        listHolder.btnSelect.setOnClickListener(onClickListener);
        listHolder.btnSelect.setTag(position);
    }

    @Override
    public int getItemCount() {
        return dataSet.size();
    }

    public void setSelectIndex(int selectIndex) {
        this.selectIndex = selectIndex;
    }

    public void setOnClickListener(View.OnClickListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    class ListHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.phone_number)
        TextView phoneNumber;
        @Bind(R.id.address)
        TextView address;
        @Bind(R.id.distance)
        TextView distance;
        @Bind(R.id.btn_select)
        ImageView btnSelect;
        @Bind(R.id.address_layout)
        RelativeLayout addressLayout;

        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


}
